/*
 * TxLineTableModel.java
 *
 * Created on April 2, 2007, 2:22 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E.gui;


import edu.byu.isys413.group1E.data.*;
import edu.byu.isys413.group1E.*;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.AbstractTableModel;
/**
 *
 * @author tkulbeth
 */
public class TxLineTableModel extends AbstractTableModel{
    
    
    String[] columnNames = {"Type","Description","Price"};                      //define the column headers
//    public int updatedRow;                                                              //counter to keep track of which row needs updated
    ArrayList rsList = new ArrayList();                                               //define the main data structure for the rows
    TransactionBO trans = null;
    
    
    public TxLineTableModel(){
    }
    
    /**Creates a new instance of TxLineTabelModel and loads the data into the table
     *from the database.
     */
    public TxLineTableModel(TransactionBO tx){
//        updatedRow = -1;
        trans = tx;
        this.loadData(trans.getTxLineList());
    }
    
    public void setTransaction(TransactionBO tx){
        this.trans = tx;
    }
    
    /** This method loads any existingrevenue source data for the table.
     */
    public void loadData(ArrayList<TransLineBO> txLines){
        ArrayList<TransLineBO> txLineList = txLines;
        rsList.clear();
        if(txLineList != null){
            for(int x=0; x<txLineList.size();x++){
                this.addRow(txLineList.get(x).getRevSrc());
                this.fireTableDataChanged();
                this.fireTableStructureChanged();
            }
        }
    }
    /**This method checks if a values in a row have been changed, and determines
     *if the database needs updated.  If so, the new values are sent to the
     *database to update the table.
     */
//    public void updateRow(int rowNum){
//        boolean needsUpdate = false;                                                        //set variable to check if a row needs updated
//        if(rowNum < dataList.size()){                                                       //check if row exists (case of last row being deleted)
//            ArrayList rowData = (ArrayList)dataList.get(rowNum);                            //get row data
//            String guid = (String)rowData.get(4);                                           //get the GUID from the row data list
//            ArrayList compareData = myDB.getRow(guid);                                      //get the existing row from the db to check against
//            for(int j=0; j<columnNames.length; j++){                                        //go through the lists and compare them
//                String tableElement = (String)rowData.get(j);
//                String dbElement = (String)compareData.get(j);
//                if(tableElement.equals(dbElement) == false){ needsUpdate = true; }          //if a value has changed, set needsUpdate to true
//            }
//            if(needsUpdate == true){                                                        //if row needs updated, then update it, otherwise, leave it be.
//                myDB.updateRow(rowData);
//                System.out.println("Row number " + rowNum + " has been updated in the database.");
//            }
//        }
//    }
//
    /*Method to get the number of rows in the table model*/
    public int getRowCount(){ return rsList.size(); }
    
    /*Method to get the number of columns in the table model data structure*/
    public int getColumnCount(){ return columnNames.length; }
    
    /*Method to retrieve a value from the data structure*/
    public Object getValueAt(int row, int col){
        ArrayList<String> thisRow = (ArrayList)rsList.get(row);                              //retrieve the row from the main data structure
        return (String)thisRow.get(col);                                                    //go through the ArrayList stored in the data structure for the column data
    }
    
    /**This method is to add a row to the data structure, according to the user
     *specification whether it is to re-use existing data from a different row.
     *
     *@param fillOpt determines if the user requested that data is to be copied from another row
     *@param rowGetData contains the position of the row from which data is to be retrieved, if the fillOpt is true
     */
    public boolean addRow(RevenueSourceBO newRS){
        ArrayList<String> newRow = new ArrayList<String>();                                            //creates new data structure
        newRow.add(newRS.getType());
        newRow.add(newRS.getDescription());
        newRow.add(Double.toString(newRS.getPrice()));
        newRow.add(newRS.getRevSrcID());
//        for(int t=0;t<=columnNames.length;t++){newData.add("");}                        //defaults the data structure to blank
        rsList.add(newRow);                                                          //add the new row to the data structure
        return true;
    }
    
    /**This method is to remove rows from the data structure, depending on the selection
     *of rows as specified by the user.
     *
     *@param rows[] contains an array of row positions to be deleted.
     */
    public void deleteRow(int[] rows){
        for(int j=rows.length-1;j>=0;j--){                                              //go through the array in reverse to avoid referencing
//            ArrayList<String> rowData = (ArrayList<String>)rsList.get(rows[j]);
//            String guid = (String)rowData.get(3);                                       //retrieve the GUID from the data entry
            trans.getTxLineList().remove(rows[j]);
        }
    }
    
    public String getColumnName(int col){return columnNames[col];}                      //renames the columns according to the columnNames array
    public boolean isCellEditable(int row, int col) { return false; }
    public void setValueAt(Object value, int row, int col){
//        ArrayList old = (ArrayList)dataList.get(row);
//        String oldData = (String)old.get(col);                                          //retrieve old data
//        old.set(col,(String)value);
//        dataList.set(row,old);                                                          //assign new data to table cell
//        fireTableCellUpdated(row,col);                                                  //tells the table that there was a permanent change to stay in the table
    }
    
    
}
